﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.Menu;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.System;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Menu.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Menu
{
    public class EditMenuService : ServiceBase, IEditMenuService
    {
        protected readonly IMenuRepository menuRepository;

        public EditMenuService(ILogger<ServiceBase> logger, IOptions<WmsSettings> options, IMapper mapper, IMenuRepository menuRepository) : base(logger, options, mapper)
        {
            this.menuRepository = menuRepository;
        }

        public async Task<ActionResultBase> EditMenu(MenuConfigDO? menuConfig, UserInfo userInfo)
        {
            var result = new ActionResultBase("保存菜单配置");
            try
            {
                if (menuConfig == null)
                    return result.ArgumentError("菜单数据不能为空");

                if (menuConfig.Id <= 0)
                    return result.ArgumentError("菜单ID不能为空");

                var menuPO = await menuRepository.FindById<MenuPO>(menuConfig.Id);
                if (menuPO == null)
                    return result.BusinessError($"ID:{menuConfig.Id},菜单数据未找到");

                mapper.Map(menuConfig, menuPO);
                var count = await menuRepository.Update(menuPO, userInfo);
                if (count == 0)
                    return result.DbError("更新菜单异常");

                result.Message = "保存菜单成功";
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
